// pages/help/help.js
import { getStorageItem, setStorageItem ,clearStorageItem} from '../../utils/storage'
import { getSharePng } from '../../config'
import {  getHostCommentBase ,saveHelperInfo} from '../../api/coupon'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    helperList: [],
    helpId: null,
    helpName: '',
    isHelper: '',
    count:0,
    helpVisible: false,
    distance: '',
    anaiClass: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.setData({
    //   anaiClass:''
    // })
    if(options.help){
      this.setData({
        helpId: options.help
      })
    }
    if(options.name){
      this.setData({
        helpName: options.name
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    if (!getApp().globalData.token) {
      setStorageItem('needReturnPage', `/pages/help/help?help=${this.data.helpId}`)
      return;
    }
    this.initHelpList(true)

    // this.setData({
    //   helpList: new Array(15).fill({
    //     avatar: 'https://thirdwx.qlogo.cn/mmopen/vi_32/bUrGSf23HibGv15wE0Zv42XnC3BDeibYqkTwxgXcz4hRLzJWxmdgJjo9u0ibbrWwEwZ80g1VFN50Wel6lrDQAER0Q/132',
    //     text: '暂无'
    //   })
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    const  danmuDom = this.selectComponent('#danmuDom')
    if (!danmuDom) {
      return;
    }
    
    clearStorageItem('needReturnPage')
    danmuDom.endLoop();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    
    if(e.from ==='button'){
      return {
        title: `${ getStorageItem('user_name')}邀请您为他助力`,
        path: `/pages/help/help?help=${getStorageItem('id')}&name=${ getStorageItem('user_name')}` ,
        imageUrl: getSharePng('pttq')
      }
    }
    else{
      return {
        title: `${getStorageItem('user_name')}邀请您来为${this.data.helpName}助力`,
        path: '/pages/help/help?help=' + this.data.helpId,
        imageUrl: getSharePng('pttq')
      }
    }

  }
  ,
  help(){
    let myId = getStorageItem('id')
    
    
    if(myId===this.data.helpId){
        wx.showToast({
          title: '您不能为自己助力',
          icon: 'none'
        })
        return ;
    }

    if(this.data.isHelper==='1'){
      wx.showToast({
        title: '已经助力过了',
        icon: 'none'
      })
      return ;  
    }

    saveHelperInfo({
      target_id: this.data.helpId
    }).then(res =>{
      
      this.setData({
        distance: res.data.distance
      })
      wx.showToast({
        title: '助力成功',
        icon: 'none'
      })
      this.initHelpList()
      this.setData({
        helpVisible: true
      })
    })
    .catch(err => {
      wx.showToast({
        title: '助力失败',
        icon: 'none'
      })
    })


   },
   initHelpList(first){

    getHostCommentBase(
      {target_id: this.data.helpId,}
    ).then(
      res => {
        
        const {helperList, isHelper,target_id } = res.data
        let count = helperList.length
        let helper = {
          headImg: '',
          nickname: '暂无'
        }
        if( helperList.length<=5){
          for(let i= helperList.length; i<5 ;i++){
            helperList.push(helper)
          }
        }
        this.setData({
          helperList,
          isHelper,
          count,
          
        })
        if(first){
          this.setData({
            anaiClass: isHelper==='1'?'reactive':''
          })
        }
      }
    )
   },
   close(){

    this.setData({
      outAnim: true
    })
    setTimeout(() => {
      this.setData({
        helpVisible: false,
        // couponType: nextCouponType,
        outAnim: false

      })
      // this.getMyCouponInfo(true);
    }, 300)
    setTimeout(()=>{
      this.setData({
        anaiClass:'reactive'
      })
    },200)
   },
   toDetail(){
     wx.switchTab({
       url: '/pages/coupon/coupon',
     })
   }
})